package com.knife.bbs.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.knife.util.CommUtil;
import com.knife.web.ActionContext;
import com.knife.web.IWebAction;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.bbs.business.ActiveUser;
import com.knife.bbs.business.BBSConfig;
import com.knife.bbs.business.BBSCoreService;
import com.knife.bbs.business.BBSDirService;
import com.knife.bbs.business.BBSRights;
import com.knife.bbs.business.BBSUserService;
import com.knife.bbs.object.BBSDir;
import com.knife.bbs.object.BBSDoc;
import com.knife.bbs.domain.UserInfo;

/**
 * 
 * <p>
 * Title:首页的页面数据
 * </p>
 * <p>
 * Description:从数据库中读取首页的相关数据!,直接实现IWebAction接口即可.
 * </p>
 */
public class FirstPageAction extends BBSAction {
	private BBSDirService dirService=BBSDirService.getInstance();
	private BBSUserService userService=BBSUserService.getInstance();
	private BBSCoreService coreService=BBSCoreService.getInstance();

	public Page doInit(WebForm form, Module module) {
		//System.out.println("执行初始化方法");
		List hotList1 = new ArrayList();// 热门信息列表1
		List hotList2 = new ArrayList();// 热点信息列表2
		List mainList = new ArrayList();// 主栏目
		List userList = new ArrayList();// 最新用户
		List newList1 = new ArrayList();//热门信息列表1
		List newList2 = new ArrayList();//热点信息列表2
		// 统计数据
		form.addResult("totalMember", coreService.getTotalMember());
		form.addResult("totalTopicNum", coreService.getTotalTopicNum());
		form.addResult("totalNum", coreService.getTotalMessageNum());
		form.addResult("todayNum", coreService.getTodayMessageNum());
		form.addResult("yesterdayNum", coreService.getYesterdayMessageNum());
		// 新进来宾
		userList = userService.getLastRegUser(5);
		List list = coreService.getHotTopic(10);
		//System.out.println(list.size());
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				BBSDoc doc = (BBSDoc) list.get(i);
				Map map = new HashMap();
				map.put("title", doc.getTitle());
				map.put("cid", doc.getCid());
				map.put("url", contextPath+doc.htmlUrl());
				map.put("readtimes", doc.getReadtimes());
				map.put("dirsn", doc.getDirsn());
				BBSDir dir =dirService.getBBSDirBySN(doc.getDirsn());
				if (dir != null) {
					map.put("dirtitle", dir.getTitle());
				}
				list.set(i,map);
			}
			List total=CommUtil.toRowChildList(list, 5);
			if(total.size()>0)
			hotList1=(List)total.get(0);
			if(total.size()>1)
			hotList2=(List)total.get(1);
		}
		list=coreService.getNewTopic(5);
		if(list!=null){
			for(int i=0;i<list.size();i++)
			{
				BBSDoc doc=(BBSDoc)list.get(i);
				Map map=new HashMap();
				map.put("title",doc.getTitle());
				map.put("cid",doc.getCid());
				map.put("readtimes",doc.getReadtimes());
				map.put("dirsn",doc.getDirsn());
				map.put("url",contextPath+doc.htmlUrl());
				map.put("inputuser",doc.getInputuser());
				BBSDir dir=dirService.getBBSDirBySN(doc.getDirsn());
				if(dir!=null)
				{
					map.put("dirtitle",dir.getTitle());				
				}
				newList1.add(map);
			}
		}
		list=coreService.getLastReplyTopic(5);
		if(list!=null){
		for(int i=0;i<list.size();i++)
		{
			BBSDoc doc=(BBSDoc)list.get(i);
			Map map=new HashMap();
			map.put("title",doc.getTitle());
			map.put("cid",doc.getCid());
			map.put("readtimes",doc.getReadtimes());
			map.put("dirsn",doc.getDirsn());
			map.put("url",contextPath+doc.htmlUrl());
			map.put("inputuser",doc.getInputuser());
			BBSDir dir=dirService.getBBSDirBySN(doc.getDirsn());
			if(dir!=null)
			{
				map.put("dirtitle",dir.getTitle());
			}
			newList2.add(map);
		}
		}
		form.addResult("hotList1", hotList1);
		form.addResult("hotList2", hotList2);
		form.addResult("newList1", newList1);
		form.addResult("newList2", newList2);
		form.addResult("newMembers", userList);
		form.addResult("hostname",BBSConfig.getInstance().getHost_name());
		return module.findPage("/"+skin+"/",module.getDefaultPage());
	}
	
	public Page doRule(WebForm form, Module module) {
		return module.findPage("/"+skin+"/","rule");
	}
}
